﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Time.Model;

namespace Time.App
{
    public partial class FindLicenseWindow : Form
    {
        private TimeDataContext db = new TimeDataContext();
        List<License> licenses = new List<License>();
        public FindLicenseWindow(Form owner)
        {
            this.Owner = owner;
            InitializeComponent();

            licenses = db.Licenses.OrderBy(l => l.Name).ToList();

            UpdateList(licenses);
        }

        private void UpdateList(List<License> licenses)
        {
            foreach (License license in licenses)
            {
                ListViewItem item = new ListViewItem();
                item.Text = license.Name;
                item.Tag = item;

                listViewLicenses.Items.Add(item);
            }
        }

        private void txtSearchForm_KeyUp(object sender, KeyEventArgs e)
        {
            List<License> foundLicences = db.Licenses.Where(l => l.Name.ToLower().Contains(txtSearchForm.Text.ToLower())).OrderBy(l => l.Name).ToList();
            UpdateList(foundLicences);
        }
    }
}
